const request = require('../utils/request');
const config = require('../config');

class GitError extends Error {
    constructor(message, payload) {
        super(message)
        this.payload = payload;
    }
}

class GiteeApi {
    constructor() {
        
    }

    async createPullRequest({owner, repo, accessToken, branch, mergeBranch}){
        console.log('createPullRequest=====', owner, repo, branch, mergeBranch);
        console.log('accessToken========', accessToken);
        return request.post(`https://gitee.com/api/v5/repos/${owner}/${repo}/pulls`, {
            access_token: accessToken,
            title: `Merge branch ${mergeBranch} into ${branch}`,
            base: branch,
            head: mergeBranch
        }).then((resp) => {
            console.log('resp1', resp);
            return resp;
        }).catch(function (error) {
            /*return {
                name: project.name,
                status: 'FAIL',
                message: error.message
            }*/
            console.log('error1', error);
            throw new GitError(error.response.data.message);
        });
    }

    async reviewPullRequest({owner, repo, number, accessToken}) {
        return request.post(`https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${number}/review`, {
            access_token: accessToken
        }).then((resp) => {
            console.log('resp2', resp);
            return resp;
        }).catch(function (error) {
            console.log('error2', error);
            throw new GitError(error.response.data.message, { prNumber: number });
        });
    }

    async mergePullRequest({owner, repo, number, accessToken}) {
        return request.put(`https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${number}/merge`, {
            access_token: accessToken,
            merge_method: 'squash'
        }).then((resp) => {
            console.log('resp3', resp);
            return resp;
        }).catch(function (error) {
            console.log('error3', error);
            throw new GitError(error.response.data.message, { prNumber: number });
        });
    }

    async testPullRequest({owner, repo, number, accessToken}) {
        return request.post(`https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${number}/test`, {
            access_token: accessToken,
        }).then((resp) => {
            console.log('resp4', resp);
            return resp;
        }).catch(function (error) {
            console.log('error4', error);
            throw new GitError(error.response.data.message, { prNumber: number });
        });
    }

    async updatePullRequest({owner, repo, number, accessToken}) {
        return request.patch(`https://gitee.com/api/v5/repos/${owner}/${repo}/pulls/${number}`, {
            access_token: accessToken,
        }).then((resp) => {
            console.log('resp5', resp);
            return resp;
        }).catch(function (error) {
            console.log('error5', error);
            throw new GitError(error.response.data.message, { prNumber: number });
        });
    }

    async fetchRemoteBranchs({owner, repo, accessToken}) {
        return request.get(`https://gitee.com/api/v5/repos/${owner}/${repo}/branches`, {
            params:{
                access_token: accessToken
            }
        })
    }

    async fetchAccessToken(code) {
        return request.post(`https://gitee.com/oauth/token?grant_type=authorization_code&code=${code}&client_id=${config.clientID}&redirect_uri=${encodeURIComponent(config.redirectUrl)}&client_secret=${config.clientSecret}`);

    }

    async refreshAccessToken(refreshToken) {
        return request.post(`https://gitee.com/oauth/token?grant_type=refresh_token&refresh_token=${refreshToken}`);
    }

    async setProtectBranch({owner, repo, branch, accessToken}) {
        return request.put(`https://gitee.com/api/v5/repos/${owner}/${repo}/branches/${branch}/protection`, {
            access_token: accessToken
        });
    }
}

class GitLabApi {
    constructor() {

    }
}

class GitOpenApi {
    constructor() {
        return new GiteeApi();
    }
}

module.exports =  GitOpenApi;